package com.xdtech.forum.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.forum.service.CommentInfoService;
import com.xdtech.forum.vo.ArticleItem;
import com.xdtech.forum.vo.CommentInfoItem;
import com.xdtech.forum.vo.MemberItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-03-06 16:52:46
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class CommentInfoController {
	@Autowired
	private CommentInfoService commentInfoService;
	@RequestMapping(params = "commentInfo")
	public ModelAndView skipCommentInfo() {
		return new ModelAndView("forum/commentInfo/commentInfo_ftl");
	}
	
	@RequestMapping(value="/commentInfo.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = commentInfoService.loadPageAndCondition(null, null);
		}else {
			results = commentInfoService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/commentInfo.do",params = "editCommentInfo")
	public ModelAndView editCommentInfo(HttpServletRequest request,Long commentInfoId) {
		if (commentInfoId!=null) {
			request.setAttribute("commentInfoItem", commentInfoService.loadCommentInfoItem(commentInfoId));
		}
		return new ModelAndView("forum/commentInfo/editCommentInfo_ftl");
	}
	
	@RequestMapping(value="/commentInfo.do",params = "saveCommentInfo")
	@ResponseBody
	public ResultMessage saveCommentInfo(CommentInfoItem item) {
		ResultMessage r = new ResultMessage();
		if (commentInfoService.saveOrUpdateCommentInfo(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/commentInfo.do",params = "deleteCommentInfoItems")
	@ResponseBody
	public ResultMessage deleteCommentInfoItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> commentInfoIds = new ArrayList<Long>();
			for (String id : tempIds) {
				commentInfoIds.add(Long.valueOf(id));
			}
			commentInfoService.deleteCommentInfoInfo(commentInfoIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/commentInfo.action",params = "myComments")
	public ModelAndView myComments(HttpServletRequest request) {
		MemberItem memberItem = (MemberItem) request.getSession().getAttribute("member");
		List<CommentInfoItem> commentInfoItems = commentInfoService.loadCommentInfosByMemberId(memberItem.getId());
		request.setAttribute("myComments", commentInfoItems);
		return new ModelAndView("forum/portal/myComments_ftl");
	}

	@RequestMapping(value="/commentInfo.action",params = "postCommentByMember")
	@ResponseBody
	public ResultMessage postCommentByMember(HttpServletRequest request,Long articleId,String content) {
		ResultMessage r = new ResultMessage();
		MemberItem memberItem = (MemberItem) request.getSession().getAttribute("member");
		commentInfoService.saveMemberPostComment(articleId,content,memberItem.getId());
		return r;
	}
	
	@RequestMapping(value="/commentInfo.action",params = "loadCommentByArticleId")
	@ResponseBody
	public ResultMessage loadCommentByArticleId(HttpServletRequest request,Long articleId) {
		ResultMessage r = new ResultMessage();
		MemberItem memberItem = (MemberItem) request.getSession().getAttribute("member");
		Map<String, Object> rs = commentInfoService.loadArticleComments(articleId,null);
		r.setObj(rs);
		return r;
	}
}
